<?php
namespace Home\Controller;
use Think\Controller;
class MessageController extends ZMController {
	public function _initialize(){
		S(array('expire'=>691200));//8小时
	}
	/*public function Message(){//数据统计
		$pdate=date('Y-m-d');//日期
		$month=date('Y-m');//月份
		$Model=M();
		$resToday=$Model->db(2,'DB_CONFIG2')->query("select sum(a.garbage+a.nocode) as num0,sum(a.lwq) as num1,sum(a.guil) as num3,sum(a.qt) as num4 from ddwk_collect as a where a.pdate='$pdate'");//今日收集
		$resMonth=$Model->db(2,'DB_CONFIG2')->query("select sum(a.garbage+a.nocode) as num0,sum(a.lwq) as num1,sum(a.guil) as num3,sum(a.qt) as num4 from ddwk_collect as a where DATE_FORMAT(a.pdate,'%Y-%m')='$month'");//月收集
		$garbage=array();
		$lwq=array();
		$guil=array();
		$hx=array();
		$qt=array();
		foreach($resToday as $row1){
			$garbage['jshouji']=$row1['num0'];
			$lwq['jshouji']=$row1['num1'];
			$guil['jshouji']=$row1['num3'];
			$hx['jshouji']=0;
			$qt['jshouji']=$row1['num4'];
		}
		foreach($resMonth as $row2){
			$garbage['yshouji']=$row2['num0'];
			$lwq['yshouji']=$row2['num1'];
			$guil['yshouji']=$row2['num3'];
			$hx['yshouji']=0;
			$qt['yshouji']=$row2['num4'];
		}
		$arr=array($garbage,$lwq,$guil,$qt,$hx);
		$this->ajax($arr);
	}
	public function daycollect(){//昨日收集量
		$arr=S('daycollect');
		if(empty($arr)){
	        $Model=M();
	        $date=date('Y-m-d',strtotime("-1 day"));
	        $res=$Model->db(2,'DB_CONFIG2')->query("select sum(a.garbage+a.nocode) as num0,sum(a.lwq) as num1,sum(a.guil) as num3,sum(a.qt) as num4 from ddwk_collect as a where a.pdate='$date'");
	        $arr=array(
	            array('name'=>'感染性废物','value'=>$res['0']['num0']),
		        array('name'=>'损伤性废物','value'=>$res['0']['num1']),
		        array('name'=>'病理性废物','value'=>$res['0']['num3']),
				array('name'=>'药物性废物','value'=>$res['0']['num4']),
	        );
		    $arr=json_encode($arr);
		    S('daycollect',$arr);
	        echo $arr;
		}
		else{
			echo $arr;
		}
    }
    public function month(){//每月收集
		$arr=S('month');
		if(empty($arr)){
		    $Model=M();
		    $date=date('Y');
		    $res=$Model->db(2,'DB_CONFIG2')->query("select month(a.pdate) as pdate,sum(a.garbage+a.qt+a.nocode+a.lwq+a.guil) as num0 from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where year(a.pdate)=$date group by month(a.pdate) order by month(a.pdate)");
            $x=array();
			$y=array();
	        foreach($res as $row){
		        $x[]=$row['pdate'].'月';
				$y[]=$row['num0'];
	        }
	        $arr=array('x'=>$x,'y'=>$y,'year'=>$date);
	        $arr=json_encode($arr);
			S('month',$arr);
	        echo $arr;
		}
		else{
			echo $arr;
		}
	}	
	public function collectNum(){//近30天量
		$user=session('user');
		$hid=$user['hid'];
		if(empty($hid)){
			exit('参数错误');
		}
		$Model=M();
		$enddate=date('Y-m-d');
		$startdate=date('Y-m-d',strtotime('-30 day'));
		$res=$Model->query("select pdate,sum(num_0+num_1+num_3+num_4+num_5) as data11 from ddwk_collect where pdate>='$startdate' and pdate<='$enddate' group by pdate order by pdate asc");
		foreach($res as &$row){
			$pdate=$row['pdate'];
			$num=$this->delrepeat($pdate,$hid);
			$row['data11']=(int)$row['data11']-$num;
		}
		$this->ajaxReturn($res);
	}*/
	public function regionCollect(){//主页
		$Model=M();
		$month=I('month');
		if(empty($month)){
		    $date=date('Y-m-d',strtotime("-1 day"));//昨日
		    $res=$Model->db(2,'DB_CONFIG2')->query("select sum(a.garbage) as garbage,sum(a.lwq) as lwq,sum(a.guil) as guil,sum(a.qt) as yw,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where a.pdate='$date' group by b.region,a.kg");
		}
		else{
			$newstr=substr($month,-2); 
			if(preg_match("/01/",$newstr)){
			    $date=date('Y-m',strtotime("-1 month"));
			}
			else{
				$date=date('Y-m');//本月
			}
			$res=S('regionCollect'.$date);
			if(empty($res)){
			    $res=$Model->db(2,'DB_CONFIG2')->query("select sum(a.garbage) as garbage,sum(a.lwq) as lwq,sum(a.guil) as guil,sum(a.qt) as yw,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where date_format(a.pdate,'%Y-%m')='$date' group by b.region,a.kg");
				S('regionCollect'.$date,$res);
			}
		}
		foreach($res as &$r){
			$kg=$r['kg'];
			$kg=explode(',',$kg);
			$r['weight']=$kg['0']/1000*$r['garbage']+$kg['1']/1000*$r['lwq']+$kg['2']/1000*$r['guil']+$kg['3']/1000*$r['yw'];
		}
		$a = array();
        foreach($res as $v){
            if(!isset($a[$v['region']])){
			    $a[$v['region']] = $v;
			}
            else {
				$a[$v['region']]['garbage'] += $v['garbage'];
				$a[$v['region']]['lwq'] += $v['lwq'];
				$a[$v['region']]['guil'] += $v['guil'];
				$a[$v['region']]['yw'] += $v['yw'];
				$a[$v['region']]['weight'] += $v['weight'];
			}
        }
		$res=array_values($a);
		$arr=array();
		foreach($res as &$row){
			$region=$row['region'];
			switch($region){
				case 0://上城区330102
				    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					$row['hx']=0;
				    $arr['330102']=$row;
					break;
				case 1://下城区330103
				    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					$row['hx']=0;
					$arr['330103']=$row;
					break;
				case 2://江干区330104
				    if(empty($arr['330104'])){
					    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					    $row['hx']=0;
					    $arr['330104']=$row;
					}
					else{
						$arr['330104']['total']+=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
						$arr['330104']['weight']+=$row['weight'];
						$arr['330104']['hx']+=0;
					}
					break;
				case 3://拱墅区330105
				    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					$row['hx']=0;
					$arr['330105']=$row;
					break;
				case 4://西湖区330106
				    if(empty($arr['330106'])){
					    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					    $row['hx']=0;
					    $arr['330106']=$row;
					}
					else{
						$arr['330106']['total']+=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
						$arr['330106']['weight']+=$row['weight'];
						$arr['330106']['hx']+=0;
					}
					break;
				case 5://滨江区330108
				    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					$row['hx']=0;
					$arr['330108']=$row;
					break;
				case 6://萧山区330109
				    if(empty($arr['330109'])){
					    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					    $row['hx']=0;
					    $arr['330109']=$row;
					}
					else{
						$arr['330109']['total']+=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
						$arr['330109']['weight']+=$row['weight'];
						$arr['330109']['hx']+=0;
					}
					break;
				case 7://余杭区330110
				    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					$row['hx']=0;
					$arr['330110']=$row;
					break;
				case 8://桐庐县330122
				    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					$row['hx']=0;
					$arr['330122']=$row;
					break;
				case 9://建德市330182
				    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					$row['hx']=0;
					$arr['330182']=$row;
					break;
				case 10://富阳市330111
				    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					$row['hx']=0;
					$arr['330111']=$row;
					break;
				case 11://临安市330185
				    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					$row['hx']=0;
					$arr['330185']=$row;
					break;
				case 12://淳安县330127
				    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					$row['hx']=0;
					$arr['330127']=$row;
					break;
				case 13://下沙经济开发区算江干
				    if(empty($arr['330104'])){
					    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					    $row['hx']=0;
					    $arr['330104']=$row;
					}
					else{
						$arr['330104']['total']+=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
						$arr['330104']['weight']+=$row['weight'];
						$arr['330104']['hx']+=0;
					}
					break;
				case 14://大江东经济开发区算萧山
				    if(empty($arr['330109'])){
					    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					    $row['hx']=0;
					    $arr['330109']=$row;
					}
					else{
						$arr['330109']['total']+=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
						$arr['330109']['weight']+=$row['weight'];
						$arr['330109']['hx']+=0;
					}	
					break;
				case 15://之江度假区算西湖
				    if(empty($arr['330106'])){
					    $row['total']=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
					    $row['hx']=0;
					    $arr['330106']=$row;
					}
					else{
						$arr['330106']['total']+=$row['garbage']+$row['lwq']+$row['guil']+$row['yw'];
						$arr['330106']['weight']+=$row['weight'];
						$arr['330106']['hx']+=0;
					}
					break;
			}
		}
		$this->ajax($arr);
	}
}